import {Component} from '@angular/core';
import {AuthService} from "../service/auth.service";
import {ToastOptions, ToastData, ToastyService} from "ng2-toasty";
import {ActivatedRoute, Router} from "@angular/router";

@Component({
  moduleId: module.id,
  selector: 'my-change-password',
  templateUrl: 'change-password.component.html',
  styleUrls: ['change-password.component.css'],
})

export class ChangePasswordComponent{

  password:string;
  repassword:string;

  constructor(public authService: AuthService, private toastyService:ToastyService,
               private route: ActivatedRoute, public router: Router) {};

  retrievePassword() {
    if (this.password !== this.repassword) {
      const toastOptions:ToastOptions = {
        title: "提示信息",
        msg: "密码不一致！",
        showClose: true,
        timeout: 1200,
        theme: 'bootstrap',
        onAdd: (toast:ToastData) => {},
        onRemove: (toast:ToastData) => {}
      };
      this.toastyService.success(toastOptions);
    }
    const token = this.route.snapshot.params['token'];

    this.authService.retrievePassword(token, this.password).subscribe( isChange => {
      let toastOptions:ToastOptions;
      if (isChange === 1) {
        toastOptions = {
          title: "提示信息",
          msg: "修改密码成功！",
          showClose: true,
          timeout: 1200,
          theme: 'bootstrap',
          onAdd: (toast:ToastData) => {},
          onRemove: (toast:ToastData) => {
            this.router.navigate(['/login']);
          }
        };
      } else {
        toastOptions = {
          title: "提示信息",
          msg: "修改密码失败",
          showClose: true,
          timeout: 1200,
          theme: 'bootstrap',
          onAdd: (toast:ToastData) => {},
          onRemove: (toast:ToastData) => {}
        };
      }
      this.toastyService.success(toastOptions);
    });
  }



}
